package redis

import (
	"context"
	"fmt"
	"strconv"
	"user/user"
)

func CartAdd(UserId, GoodsId, Stock int, Title string) error {

	sprintf := fmt.Sprintf("cart:%d:%d", UserId, GoodsId)
	return Client.HMSet(context.Background(), sprintf, map[string]interface{}{
		"UserId":  UserId,
		"GoodsId": GoodsId,
		"Stock":   Stock,
		"Title":   Title,
	}).Err()

}
func CartList(UserId, GoodsId int) (CartList []*user.CartListStruct) {

	sprintf := fmt.Sprintf("cart:%d:%d", UserId, GoodsId)
	keys := Client.Keys(context.Background(), sprintf).Val()

	for _, v := range keys {
		HGetAll := Client.HGetAll(context.Background(), v).Val()
		UserId, _ = strconv.Atoi(HGetAll["UserId"])
		GoodsId, _ = strconv.Atoi(HGetAll["GoodsId"])
		Stock, _ := strconv.Atoi(HGetAll["Stock"])
		Title, _ := HGetAll["Title"]

		CartList = append(CartList, &user.CartListStruct{
			UserId:  int64(UserId),
			GoodsId: int64(GoodsId),
			Stock:   int64(Stock),
			Title:   Title,
		})

	}
	return
}
